package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.pojo.CustomerReprieve;
import com.example.mapper.CustomerReprieveMapper;
import com.example.pojo.SaleChance;
import com.example.service.ICustomerReprieveService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.util.AssertUtil;
import com.example.vo.ResultVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jayway.jsonpath.internal.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 客户流失暂缓表 服务实现类
 * </p>
 *
 * @author wsl
 * @since 2022-05-10
 */
@Service
public class CustomerReprieveServiceImpl extends ServiceImpl<CustomerReprieveMapper, CustomerReprieve> implements ICustomerReprieveService {
    @Autowired
    private CustomerReprieveMapper customerReprieveMapper;
    /**
     * 无条件分页
     */
    public PageInfo<CustomerReprieve> electByPager(Integer pageNo, Integer pageSize){
        //1、启动分页
        PageHelper.startPage(pageNo,pageSize);
        QueryWrapper<CustomerReprieve> wrapper = new QueryWrapper<CustomerReprieve>().orderByDesc("id");
        //2、紧接着查询操作
        List<CustomerReprieve> list = baseMapper.selectList(wrapper);
        //3、组装
        return new PageInfo<>(list);
    }
    /**
     * 查询所有
     */
    public List<CustomerReprieve> selectAll(){
        QueryWrapper<CustomerReprieve> wrapper = new QueryWrapper<CustomerReprieve>().orderByDesc("id");
        return customerReprieveMapper.selectList(wrapper);
    }
    /**
     * 添加暂缓
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public ResultVo addCustomerReprieve(CustomerReprieve customerReprieve){
        //效验参数
        checkPlan(customerReprieve);
        //createDate创建时间    默认为当前系统时间
        customerReprieve.setCreateDate(new Date());
        //updateDate更新时间    默认为当前系统时间
        customerReprieve.setUpdateDate(new Date());
        int i = baseMapper.insert(customerReprieve);
        return i > 0 ? ResultVo.ok("添加成功！") : ResultVo.error("添加失败！");
    }
    public void checkPlan(CustomerReprieve customerReprieve){
        //measure暂缓措施       非空
        AssertUtil.isTrue(Utils.isEmpty(customerReprieve.getMeasure()),"暂缓措施不能为空！");
    }

}
